<?php

namespace app\api\controller;

use app\common\model\MallConfig as MallConfigModel;
use app\common\model\Live;
use app\common\model\Daily;
use app\service\MallConfigService;

class Config extends Base
{
    public function jssdk()
    {
        $input = input();
        Daily::inc('pv');
        if ($input['url']) {
            $this->officialAccount->jssdk->setUrl($input['url']);
        }
        $api = [];
        if ($input['jsApiList']) {
            $api = $input['jsApiList'];
        }
        return json_decode($this->officialAccount->jssdk->buildConfig($api, $debug = false, $beta = false, $json = true), true);
    }

    /* 全局配置 */
    public function index()
    {
        $config = MallConfigModel::getConfig($this->mall_id);
        $mallDetail = json_decode($this->mall['detail'], true);
        $result = [
            'share_title' => $config['share_title'],
            'share_picture' => tomedia($config['share_picture']),
            'share_desc' => $config['share_desc'],
            'shop_name' => $config['shop_name'],
            'shop_tel' => $config['shop_tel'],
            'shop_open' => $config['shop_open'],
            'shop_advert' => $config['shop_advert'],
            'shop_close_reason' => $config['shop_close_reason'],
            'credit_money' => '余额',
            'credit_score' => '积分',
            'credit_cash' => '抵扣金',
            'finance_atm_help' => nl2br(config('finance_atm_help')),
            'chat_room_id' => config('chat_room_id'),
            'chat_forbidden' => config('chat_forbidden') ? config('chat_forbidden') : 0,
            'shop_ad_picture' => tomedia($config['shop_ad_picture']),
            'shop_ad_video' => tomedia($config['shop_ad_video']),
            'shop_qrcode' => tomedia($config['shop_qrcode']),//公众号关注二维码
            'open_qrcode' => (int)$config['open_qrcode'],
            'open_mobile' => (int)$config['open_mobile'],
            'site_menu' => json_decode(config('site_menu'), true),
            'chat_view' => $config['chat_view'],
            'daysign' => config('site_daysign_max') == 0 ? 0 : 1,//每日签到开关
            'redbag_type' => $config['redbag_type'],   //抢红包存储设置 1:余额 2:抵扣金
            'dikou_rate' => $config['dikou_rate'],   //如果是抵扣金兑换比例
            'chat_announce' => $config['chat_announce'],   //群聊天公告
            'chat_bg_picture' => tomedia($config['chat_bg_picture']), //聊天室背景图片
            "register_msg" => config('register_msg'),
            "bug_log" => config('bug_log'),//更新日志
            'transfer_rule' => config('transfer_rule'),// 转账提示
            'active_advert' => $config['active_advert'],//活动滚动提示
            'active_gl' => config('active_gl'),// 活动攻略
        ];
        if (config('aliyun_live_open') == 1) {
            $liveInfo = Live::getLiveInfo();
            if (config('aliyun_live_default_pull')) {
                $liveInfo['pull']['m3u8'] = config('aliyun_live_default_pull');
            }
            $result['liveInfo'] = $liveInfo['pull'];
        } else {
            $result['liveInfo'] = false;
        }
        $this->ajax(200, 'success', $result);
    }

    /**
     * 修改分公司配置项
     */
    public function updateMall()
    {
        $input = input('');
        if (!\app\common\model\User::isManager($this->uid))
            $this->ajax(202, '您无权修改公告提示信息');
        if (!isset($input['name']) || empty($input['name']))
            $this->ajax(202, '请输入配置项名称');
        if (!isset($input['value']) || empty($input['value']))
            $this->ajax(202, '请输入配置项的值');
        $where = array(
            'mall_id' => ['eq', $this->mall_id],
            'name' => ['eq', $input['name']]
        );
        $config = MallConfigModel::getOne($where);
        $data = [
            'mall_id' => $this->mall_id,
            'name' => $input['name'],
            'value' => $input['value'],
        ];
        if ($config) {
            $res = MallConfigService::S()->add($data, $config['id']);
        } else {
            $res = MallConfigService::S()->add($data);
        }
        if ($res) {
            $this->ajax(200, '修改成功', $res);
        } else {
            $this->ajax(202, '修改失败');
        }
    }
}
